package com.yenlien.traceyun.breed.controller;

import com.yenlien.traceyun.breed.entity.Quarantine;
import com.yenlien.traceyun.breed.service.BreedTraceInfoService;
import com.yenlien.traceyun.breed.service.QuarantineService;
import com.yenlien.traceyun.business.interceptor.UserAuthUtil;
import com.yenlien.traceyun.webase.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by LiuSha on 2020/9/25.
 */
@Api(tags = "养殖检疫管理")
@RestController
@RequestMapping("/quarantine")
public class QuarantineController extends BaseController {
    @Autowired
    private QuarantineService quarantineService;

    @Autowired
    private BreedTraceInfoService breedTraceInfoService;

    @RequestMapping(value = "/findByList", method = RequestMethod.POST)
    @ApiOperation(value = "列表", notes = "参数示例：{\"page\":\"1\",\"query\":\"BC20200923\"}")
    public Object findByList(@RequestBody Quarantine quarantine) {
        quarantine.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        return buildPage(quarantineService.findByList(quarantine));
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ApiOperation(value = "新增或修改", notes = "新增或修改时将整个对象传过来，修改时将要修改的整个对象获取到，把需要修改的字段修改就行")
    public Object save(Quarantine quarantine, HttpServletRequest request) throws Exception {
        List<MultipartFile> files = new ArrayList<>();
        if (!StringUtils.isEmpty(((MultipartHttpServletRequest) request).getFile("files"))) {
            files = ((MultipartHttpServletRequest) request).getFiles("files");
        }
        quarantine.setCompanyId(UserAuthUtil.getLoginUser().getCompanyId());
        quarantineService.save(quarantine, files);
        breedTraceInfoService.subGenerateCodeInfo(quarantine.getBreedId());
        return success("success");
    }

    @RequestMapping(value = "/findById/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "查看", notes = "")
    public Object findById(@PathVariable String id) {
        return success(quarantineService.findById(id));
    }
}
